package com.vtron.dbase.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vtron.dbase.dao.IBaseDao;
import com.vtron.dbase.models.DbRelationship;
import com.vtron.dbase.models.PageResult;
import com.vtron.dbase.service.IBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @date : 2021/12/28 16:03
 * @author: 忻龙彪
 */

public class BaseServiceImpl<T> implements IBaseService<T> {
    public IBaseDao<T> getRepositoryDao() {
        throw new RuntimeException("repositoryDao not implemented!");
    }


    @Override
    public void save(T t) throws Exception {
        getRepositoryDao().save(t);
    }

    @Override
    public void update(T t) throws Exception {
        getRepositoryDao().update(t);
    }

    @Override
    public void delete(String id) throws Exception {
        getRepositoryDao().delete(id);
    }

    @Override
    public T get(String id) throws Exception {
        return getRepositoryDao().get(id);
    }

    @Override
    public List<T> list(DbRelationship qParameter) throws Exception {
        return list(new ArrayList<DbRelationship>() {{
            add(qParameter);
        }});
    }

    @Override
    public List<T> list(List<DbRelationship> qParameter) throws Exception {
        return getRepositoryDao().list(qParameter);
    }

    @Override
    public PageResult pages(DbRelationship qParameters, Integer pageNum, Integer pageSize) throws Exception {
        return pages(new ArrayList<DbRelationship>() {{
            add(qParameters);
        }},pageNum,pageSize);
    }


    @Override
    public PageResult pages(List<DbRelationship> qParameter, Integer pageNum, Integer pageSize) throws Exception {
        if (pageNum.equals(null)) {
            throw new Exception("pageNum is null");
        }
        if (pageSize.equals(null)) {
            throw new Exception("pageSize is null");
        }
        PageHelper.startPage(pageNum, pageSize);
        return new PageResult(new PageInfo<T>(list(qParameter)));
    }
}
